package com.xusheng.cli.core.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xusheng.cli.entity.frontUserModule.FrontUser;
import com.xusheng.cli.entity.vipLevelModule.VipLevel;
import com.xusheng.cli.mapper.FrontUserMapper;
import com.xusheng.cli.mapper.VipLevelMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 王鹤然
 * @since 2020-10-25 01:47
 */
@Component
@Slf4j
public class UpdateTodaySignTask {


    @Autowired
    private FrontUserMapper frontUserMapper;
    @Autowired
    private VipLevelMapper vipLevelMapper;


    /**
     * 每天更新签到状态
     *
     * @author 王鹤然
     * @date 2022/11/25 0025 下午 17:27
     */
    @Async("taskExecutor")
    @Scheduled(cron = "0 0 0 * * ? ")
    public void updateTodaySign() {
        // 重置签到
        frontUserMapper.updateTodaySign();
        // 重置每日观看次数
        // 获取信息
        List<FrontUser> frontUsers = frontUserMapper.selectList(new QueryWrapper<>());
        for (FrontUser frontUser : frontUsers) {
            Long vipId = frontUser.getPromotionLevel();
            VipLevel vipLevel = vipLevelMapper.selectById(vipId);
            if (vipLevel != null) {
                frontUser.setWatchCount(vipLevel.getWatchCount());
                frontUser.setWatchCountShort(vipLevel.getWatchCountShort());
                frontUserMapper.updateById(frontUser);
            }
        }
        log.info("执行定时任务:updateTodaySign");
    }

}
